// pages/apply/index.js
import {
    venue,
    common
} from '../../api/apiData'
let util = require('../../utils/util.js')

Page({

    /**
     * 页面的初始数据
     */
    data: {
        info: '',
        periodList: [],
        dateSel: '',
        period_id: [],
        captcha_code: '',
        captcha_id: '',
        people_num: '',
        fileList: [],
        fileList2: [],
        material_list: [],
        arr: [], //必填项下标
        imageList: [], //图片上传的下标
        id: '',
        type: '', //文本类型 
        inputList: [], //文本输入值
        columnsNew: [],
        maxlength: -1, //最大长度
        idx: -1, //弹窗选项下标
        keyList: [], //key
        currentDate: new Date().getTime(),
        minDate: new Date('1900-01-01').getTime(),
        captcha_img: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            venue_id: options.id,
            book_date: options.date
        })
        let that = this
        let {
            inputList,
            keyList
        } = that.data;
        venue.detail({
            venue_id: options.id,
            book_date: options.date
        }, res => {
            this.setData({
                info: res.data
            })
            if (res.data.is_write == 1) {
                let formTable = res.data.form_data;
                let arr = [];
                let imageList = [];
                formTable.forEach((item, index) => {
                    if (item.type == 'input' || item.type == 'date' || item.type == 'textarea') {
                        inputList[index] = item.options.defaultValue
                    }
                    if (item.type == 'radio' || item.type == 'select') {
                        if (item.options.defaultValue != '') {
                            item.options.options.forEach(it => {
                                if (it.value == item.options.defaultValue) {
                                    inputList[index] = it.label
                                    keyList[index] = it.value
                                }
                            })
                            // inputList[index] = item.options.options[]

                        }
                    }
                    // 获取必填表单
                    if (item.options.required) {
                        arr.push(index)

                    }
                    // 获取图片上传表单
                    if (item.type == 'imgupload') {
                        imageList.push(index)
                    }
                })
                console.log(inputList[6])
                that.setData({
                    formTable: formTable,
                    arr,
                    title: res.data.name,
                    imageList,
                    inputList,
                    keyList
                })
            }
            if (res.data.is_captcha == 1) {
                this.getCapt()
            }
        })
        venue.period.lists({
            venue_id: options.id,
            date: options.data
        }, res => {
            this.setData({
                periodList: res.data,
                period_id: res.data[0].id
            })
        })
    },
    toCall(){
        let phone = this.data.info.telephone
        wx.makePhoneCall({
          phoneNumber: phone,
        })
    },
    getCapt() {
        common.captcha({}, res => {
            this.setData({
                captcha_id: res.data.uniqid,
                captcha_img: res.data.content
            })
        })
    },
    toDate(e) {
        let id = e.currentTarget.dataset.id
        this.setData({
            period_id: id
        })
    },
    submit(e) {
        let obj = {}
        let that = this
        let {
            value
        } = e.detail;
        const {
            id,
            arr,
            inputList
        } = that.data;
        // 校验是否必填
        try {
            arr.forEach(item => {
                if (inputList[item] == '' || inputList[item] == undefined) {
                    throw Error('请将标有*的必填项信息补充完整~')
                }
            })
        } catch (e) {
            wx.showToast({
                title: e.message,
                icon: 'error'
            })
            return
        }
        obj.apply_data = value
        obj.venue_id = that.data.venue_id
        obj.period_id = that.data.period_id
        obj.people_num = that.data.people_num
        obj.book_date = that.data.book_date
        if (that.data.info.is_captcha == 1) {
            obj.captcha_id = that.data.captcha_id
            obj.captcha_code = util.AesEncode(that.data.captcha_code)
        }
        console.log(obj)
        venue.order.submit(obj, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg
                })
                setTimeout(() => {
                    wx.navigateTo({
                        url: '/pages/apply/list',
                    })
                }, 1000)
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    toSubmit(){
        venue.order.submit({}, res => {
            if (res.code == 1) {
                wx.showToast({
                    title: res.msg
                })
                setTimeout(() => {
                    wx.navigateTo({
                        url: '/pages/apply/list',
                    })
                }, 1000)
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'error'
                })
            }
        })
    },
    // 获取文本输入值
    onInputChange(e) {
        const that = this;
        const {
            index
        } = e.currentTarget.dataset;
        let {
            inputList
        } = that.data;
        let {
            detail
        } = e;
        inputList[index] = detail.replace(/[, ]/g, '');
        that.setData({
            inputList
        })
    },
    // 弹窗
    getInput(e) {
        const that = this;
        const {
            type,
            options,
            index
        } = e.currentTarget.dataset;
        if (options.length != 0) {
            options.forEach(item => {
                if (item.label) {
                    that.setData({
                        selectL: true
                    })
                } else {
                    that.setData({
                        selectL: false
                    })
                }
            })
        }

        if (type == 'radio' || type == 'select' || type == 'checkbox') { //单选  弹窗选择

            that.setData({
                columnsNew: options,
                showNew: true,
                idx: index,
                type
            })
        }
        if (type == "time" || type == "date") { //日期选择 弹窗选择
            that.setData({
                showNew: true,
                idx: index,
                type
            })
        }
    },
    //  弹窗确定
    onConfirmNew(e) {
        const that = this;
        const {
            idx,
            keyList,
            inputList,
            selectL
        } = that.data;
        let {
            type
        } = e.currentTarget.dataset;
        let value;
        let key;
        if (type == 'radio') {
            // 单选
            value = e.detail.value.label;
            key = e.detail.value.value;
        } else if (type == 'select') {
            console.log(e.detail)
            if (selectL) {
                // 单选
                value = e.detail.value.label;
                key = e.detail.value.value;
            } else {
                // 单选
                value = e.detail.value.value;
                key = e.detail.value.value;
            }

        } else { //日期
            value = getFormat(new Date(e.detail), 'yy-mm-ss');
            key = getFormat(new Date(e.detail), 'yy-mm-ss');
        }
        keyList[idx] = key
        inputList[idx] = value;
        that.setData({
            keyList,
            showNew: false,
            inputList
        })
    },
    // 关闭弹窗
    onCancelNew() {
        this.setData({
            showNew: false
        })
    },
    onClickHide() {
        this.setData({
            showSign: false
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})